package com.bjc.platform.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

public class CustomUserDetails implements UserDetails {

	private static final long serialVersionUID = 4662410530893763345L;
	private Long id;
	private String username;
	private String password;
	private AdmOrg org;
	private Set<AdmRole> roles;
	private AdmOrg operCompany;
	private List<Object> urlRes; // 用户可访问的URL资源

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}


	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String getPassword() {
		return this.password;
	}

	@Override
	public String getUsername() {
		return this.username;
	}

	@Override
	public boolean isAccountNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return true;
	}

	public Collection<? extends GrantedAuthority> getAuthorities() {
		return null;
	}

	public AdmOrg getOrg() {
		return org;
	}

	public void setOrg(AdmOrg org) {
		this.org = org;
	}

	public Set<AdmRole> getRoles() {
		return roles;
	}

	public void setRoles(Set<AdmRole> roles) {
		this.roles = roles;
	}

	public List<Object> getUrlRes() {
		return urlRes;
	}

	public void setUrlRes(List<Object> list) {
		this.urlRes = list;
	}

	public AdmOrg getOperCompany() {
		if (operCompany != null && operCompany.getId() != null) {
			return this.operCompany;
		} else if (!"GROUP".equalsIgnoreCase(org.getOrgnizeType())) {
			return org.getCompany();
		} else {
			return org;
		}

	}

	public void setOperCompany(AdmOrg operCompany) {
		this.operCompany = operCompany;
	}

	/**
	 * 返回当前用户的组织描述
	 * 
	 * @return
	 */
	public String getOrgDesc() {
		List<String> arrOrgNames = new ArrayList<String>();
		if (this.org != null) {
			arrOrgNames.add(this.org.getName());
			AdmOrg p = org.getParent();
			while (p != null) {
				arrOrgNames.add(p.getName());
				p = p.getParent();
			}
			String[] orgNames = arrOrgNames.toArray(new String[arrOrgNames.size()]);
			ArrayUtils.reverse(orgNames);
			StringBuilder sb = new StringBuilder();
			boolean ff = true;
			for (String name : orgNames) {
				if (!ff) {
					ff = false;
					sb.append("->");
				}
				sb.append(name);
			}
			return sb.toString();
		} else {
			return "";
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CustomUserDetails other = (CustomUserDetails) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
}
